package com.mybatisflex.test.util;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
 * @Author Linbookz
 * @Date 2023/12/29
 * @Description 简易版本生成器
 * @Param
 * @Return
 */
public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex-test?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //基础包路径
        globalConfig.setBasePackage("com.mybatisflex.test");
        globalConfig.setEntityPackage("com.mybatisflex.test"+".model.entity");
        globalConfig.setMapperXmlPath(globalConfig.getSourceDir() + "/../resources/mapper");
        //java doc
        globalConfig.getJavadocConfig().setAuthor("linbookz");



        //设置表前缀和只生成哪些表
        globalConfig.setTablePrefix("tb_");
        globalConfig.setGenerateTable("tb_account");//"tb_account", "tb_account_session"

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);

        //mapper
        globalConfig.enableMapper();

        //mapper.xml
        globalConfig.enableMapperXml();

        //service
        globalConfig.enableService().setClassPrefix("I");
        globalConfig.enableServiceImpl();
//        globalConfig.enableServiceImpl().setSuperClass(BaseServiceImpl.class);

        //controller
        globalConfig.enableController();
        //TableDef
        globalConfig.enableTableDef();

  /*      //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.setColumnConfig("tb_account", columnConfig);
*/
        return globalConfig;
    }


}
